package com.lu.dog.dbrowser.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.lu.adog.util.Logg;
import com.lu.dog.dbrowser.bean.IconInfo;
import com.lu.dog.dbrowser.bean.LogInfo;
import com.lu.dog.dbrowser.bean.SearchInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Yue
 * @date 2017/12/22 18:44
 */

public class DBrowserDbHelper extends BaseDbHelper {

    private static final String name = "dbrowser.db";
    private static final int version = 1;


    private static DBrowserDbHelper INSTANCE;

    public static DBrowserDbHelper getInstance(Context context) {
        if (INSTANCE == null) {
            INSTANCE = new DBrowserDbHelper(context);
        }
        return INSTANCE;
    }

    private DBrowserDbHelper(Context context) {
        super(context, name, null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE \"searchInfo\" (\"id\"  INTEGER NOT NULL,\"content\"  TEXT,\"description\"  TEXT,\"createTime\"  TEXT,\"updateTime\"  TEXT,PRIMARY KEY (\"id\"),UNIQUE (\"content\"));");
        db.execSQL("CREATE TABLE \"iconInfo\" (\"id\"  INTEGER NOT NULL,\"url\"  TEXT NOT NULL,\"name\" TEXT,\"imageUrl\"  TEXT,\"imageData\"  TEXT,\"createTime\"  TEXT,\"updateTime\"  TEXT,PRIMARY KEY (\"id\"),UNIQUE (\"url\"));");
        db.execSQL("CREATE TABLE \"logInfo\" (\"id\"  INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\"title\"  TEXT,\"summary\"  TEXT(40) NOT NULL,\"content\"  TEXT,\"createTime\"  LONG,\"type\"  INTEGER);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public List<IconInfo> selectAllIconInfo() {
        Cursor cursor = selectAll("iconInfo");
        List<IconInfo> datas = new ArrayList<>();

        while (cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String url = cursor.getString(cursor.getColumnIndex("url"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String imageUrl = cursor.getString(cursor.getColumnIndex("imageUrl"));
            String imageData = cursor.getString(cursor.getColumnIndex("imageData"));
            long createTime = cursor.getLong(cursor.getColumnIndex("createTime"));
            long updateTime = cursor.getLong(cursor.getColumnIndex("updateTime"));

            IconInfo iconInfo = new IconInfo(id, url, name, imageUrl, imageData, createTime, updateTime);
            Logg.w(">>>", iconInfo + "");
            datas.add(iconInfo);
        }
        cursor.close();
        return datas;
    }

    public List<SearchInfo> selectAllSearchInfo() {
        Cursor cursor = selectAll("searchInfo", "updateTime", true);
        List<SearchInfo> datas = new ArrayList<>();

        while (cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String content = cursor.getString(cursor.getColumnIndex("content"));
            String description = cursor.getString(cursor.getColumnIndex("description"));
            long createTime = cursor.getLong(cursor.getColumnIndex("createTime"));
            long updateTime = cursor.getLong(cursor.getColumnIndex("updateTime"));

            datas.add(new SearchInfo(id, content, description, createTime, updateTime));
        }
        cursor.close();
        return datas;
    }

    public List<LogInfo> selectAllLogInfo() {
        List<LogInfo> logInfoList = new ArrayList<>();
        Cursor cursor = selectAll("logInfo", "createTime", true);
        while (cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String title = cursor.getString(cursor.getColumnIndex("title"));
            String summary = cursor.getString(cursor.getColumnIndex("summary"));
            String conent = cursor.getString(cursor.getColumnIndex("content"));
            long createTime = cursor.getLong(cursor.getColumnIndex("createTime"));
            int type = cursor.getInt(cursor.getColumnIndex("type"));

            logInfoList.add(new LogInfo(id, title, summary, conent, createTime, type));
        }
        cursor.close();
        return logInfoList;
    }

    public List<LogInfo> selectAllLogInfoSummary() {
        List<LogInfo> logInfoList = new ArrayList<>();
        SQLiteDatabase db = getReadableDatabase();


        Cursor cursor = db.rawQuery("SELECT id,title,summary,createTime,type FROM \"logInfo\" ORDER BY createTime DESC;", null);

        while (cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String title = cursor.getString(cursor.getColumnIndex("title"));
            String summary = cursor.getString(cursor.getColumnIndex("summary"));
            long createTime = cursor.getLong(cursor.getColumnIndex("createTime"));
            int type = cursor.getInt(cursor.getColumnIndex("type"));

            logInfoList.add(new LogInfo(id, title, summary, null, createTime, type));

        }
        cursor.close();
        return logInfoList;
    }

    public LogInfo selectLogInfoById(int val) {
        Cursor cursor = selectByColumnVal("logInfo", "id", val);
        LogInfo logInfo = null;
        if (cursor.moveToFirst()) {
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String title = cursor.getString(cursor.getColumnIndex("title"));
            String summary = cursor.getString(cursor.getColumnIndex("summary"));
            String content = cursor.getString(cursor.getColumnIndex("content"));
            long createTime = cursor.getLong(cursor.getColumnIndex("createTime"));
            int type = cursor.getInt(cursor.getColumnIndex("type"));
            logInfo = new LogInfo(id, title, summary, content, createTime, type);
        }
        return logInfo;

    }

    public void deleteAllSearchInfo() {
        deleteTable("searchInfo");
    }

    public void deleteAllIconInfo() {
        deleteTable("iconInfo");
    }

    public void deleteAllLogInfoList() {
        deleteTable("logInfo");
    }

    public void deleteSearchInfo(SearchInfo searchInfo) {
        delectARow("searchInfo", "content", searchInfo.getContent() + "");
    }

    public void deleteLogInfo(LogInfo logInfo) {
        delectARow("logInfo", "id", logInfo.getId() + "");
    }

    public void deleteIconInfo(IconInfo iconInfo) {
//        表名加双引号，则表中即使没有字段也不会报错。不加则报
        delectARow("iconInfo", "\"url\"", iconInfo.getUrl() + "");
    }

    public void updateSearchInfo(SearchInfo searchInfo) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues cv = getContentValues(searchInfo);
        boolean flag = selectIfHave("searchInfo", "content", searchInfo.getContent() + "");
        if (flag) {
            db.update("searchInfo", cv, "content=?", new String[]{searchInfo.getContent() + ""});
        } else {
            db.insert("searchInfo", null, cv);
        }
    }

    public void updateIconInfo(IconInfo iconInfo) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues cv = getContentValues(iconInfo);
        boolean flag = selectIfHave("iconInfo", "url", iconInfo.getUrl() + "");
        if (flag) {
            db.update("iconInfo", cv, "url=?", new String[]{iconInfo.getUrl() + ""});
        } else {
            db.insert("iconInfo", null, cv);
        }

    }

    public void updateIconInfo(List<IconInfo> iconInfos) {
        SQLiteDatabase db = getWritableDatabase();
        db.beginTransaction();
        for (IconInfo iconInfo : iconInfos) {
            ContentValues cv = getContentValues(iconInfo);
            boolean flag = selectIfHave("iconInfo", "url", iconInfo.getUrl() + "");
            if (flag) {
                db.update("iconInfo", cv, "url=?", new String[]{iconInfo.getUrl() + ""});
            } else {
                db.insert("iconInfo", null, cv);
            }
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        db.close();
    }

    public void updateLogInfo(LogInfo logInfo) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues cv = getContentValues(logInfo);
        db.insert("logInfo", null, cv);
    }

    public void updateLogInfo(List<LogInfo> logInfos) {
        SQLiteDatabase db = getWritableDatabase();
        db.beginTransaction();
        for (LogInfo logInfo : logInfos) {
            ContentValues cv = getContentValues(logInfo);
            db.insert("logInfo", null, cv);
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        db.close();
    }

    private ContentValues getContentValues(SearchInfo searchInfo) {
        ContentValues cv = new ContentValues();
        cv.put("content", searchInfo.getContent());
        cv.put("description", searchInfo.getDescription());
        cv.put("createTime", searchInfo.getCreateTime());
        cv.put("updateTime", searchInfo.getUpdateTime());
        return cv;
    }

    private ContentValues getContentValues(IconInfo iconInfo) {
        ContentValues cv = new ContentValues();
        cv.put("url", iconInfo.getUrl());
        cv.put("name", iconInfo.getName());
        cv.put("imageUrl", iconInfo.getImageUrl());
        cv.put("imageData", iconInfo.getImageData());
        cv.put("createTime", iconInfo.getCreateTime());
        cv.put("updateTime", iconInfo.getUpdateTime());
        return cv;
    }

    private ContentValues getContentValues(LogInfo logInfo) {
        ContentValues cv = new ContentValues();
        cv.put("title", logInfo.getTitle());
        cv.put("summary", logInfo.getSummary());
        cv.put("content", logInfo.getContent());
        cv.put("createTime", logInfo.getCreateTime());
        cv.put("type", logInfo.getType());
        return cv;
    }

}
